﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CodeAuto.DALS
{
    public class CsprojDALS
    {
        public static string GetCsproject(string projectName, List<Models.T_Table> tables, int pageType, int projectId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine("<Project ToolsVersion=\"4.0\" DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">");
            sb.AppendLine(" <PropertyGroup>");
            sb.AppendLine("     <Configuration Condition=\" '$(Configuration)' == '' \">Debug</Configuration>");
            sb.AppendLine("     <Platform Condition=\" '$(Platform)' == '' \">AnyCPU</Platform>");
            sb.AppendLine("     <ProductVersion>8.0.30703</ProductVersion>");
            sb.AppendLine("     <SchemaVersion>2.0</SchemaVersion>");

            sb.AppendLine("     <ProjectGuid>{B353B858-7123-4C98-" + GetPartString(projectId, pageType, 4) + "-" + GetPartString(projectId, pageType, 12) + "}</ProjectGuid>");
            //sb.AppendLine("     <ProjectGuid></ProjectGuid>");
            sb.AppendLine("     <OutputType>Library</OutputType>");
            sb.AppendLine("     <AppDesignerFolder>Properties</AppDesignerFolder>");
            if (pageType == 1)
            {
                sb.AppendLine("     <RootNamespace>" + projectName + ".Models</RootNamespace>");
                sb.AppendLine("     <AssemblyName>" + projectName + ".Models</AssemblyName>");
            }
            else if (pageType == 2)
            {
                sb.AppendLine("     <RootNamespace>" + projectName + ".Interfaces</RootNamespace>");
                sb.AppendLine("     <AssemblyName>" + projectName + ".Interfaces</AssemblyName>");
            }
            else
            {
                sb.AppendLine("     <RootNamespace>" + projectName + ".Impls</RootNamespace>");
                sb.AppendLine("     <AssemblyName>" + projectName + ".Impls</AssemblyName>");
            }

            sb.AppendLine("     <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>");
            sb.AppendLine("     <FileAlignment>512</FileAlignment>");
            sb.AppendLine(" </PropertyGroup>");
            sb.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' \">");
            sb.AppendLine("     <DebugSymbols>true</DebugSymbols>");
            sb.AppendLine("     <DebugType>full</DebugType>");
            sb.AppendLine("     <Optimize>false</Optimize>");
            sb.AppendLine("     <OutputPath>bin\\Debug\\</OutputPath>");
            sb.AppendLine("     <DefineConstants>DEBUG;TRACE</DefineConstants>");
            sb.AppendLine("     <ErrorReport>prompt</ErrorReport>");
            sb.AppendLine("     <WarningLevel>4</WarningLevel>");
            sb.AppendLine(" </PropertyGroup>");
            sb.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' \">");
            sb.AppendLine("     <DebugType>pdbonly</DebugType>");
            sb.AppendLine("     <Optimize>true</Optimize>");
            sb.AppendLine("     <OutputPath>bin\\Release\\</OutputPath>");
            sb.AppendLine("     <DefineConstants>TRACE</DefineConstants>");
            sb.AppendLine("     <ErrorReport>prompt</ErrorReport>");
            sb.AppendLine("     <WarningLevel>4</WarningLevel>");
            sb.AppendLine(" </PropertyGroup>");
            sb.AppendLine(" <ItemGroup>");
            sb.AppendLine("     <Reference Include=\"System\" />");
            sb.AppendLine("     <Reference Include=\"System.Core\" />");
            sb.AppendLine("     <Reference Include=\"System.Xml.Linq\" />");
            sb.AppendLine("     <Reference Include=\"System.Data.DataSetExtensions\" />");
            sb.AppendLine("     <Reference Include=\"Microsoft.CSharp\" />");
            sb.AppendLine("     <Reference Include=\"System.Data\" />");
            sb.AppendLine("     <Reference Include=\"System.Xml\" />");
            sb.AppendLine(" </ItemGroup>");
            sb.AppendLine(" <ItemGroup>");
            foreach (Models.T_Table t in tables)
            {
                if (pageType == 1)
                {
                    sb.AppendLine("     <Compile Include=\"" + t.TableName + ".cs\" />");
                }
                else if (pageType == 2)
                {
                    sb.AppendLine("     <Compile Include=\"I" + t.TableName + ".cs\" />");
                }
                else
                {
                    sb.AppendLine("     <Compile Include=\"Impl" + t.TableName + ".cs\" />");

                }
            }
            sb.AppendLine("    <Compile Include=\"Properties\\AssemblyInfo.cs\" />");
            sb.AppendLine(" </ItemGroup>");
            //if (pageType == 2)
            //{
            //    sb.AppendLine(" <ItemGroup>");
            //    sb.AppendLine("     <ProjectReference Include=\"..\\" + projectName + ".Models\\" + projectName + ".Models.csproj\">");
            //    sb.AppendLine("     <Project>{B353B858-7123-4C98-" + GetPartString(projectId, 1, 4) + "-" + GetPartString(projectId, 1, 12) + "}</Project>");
            //    //sb.AppendLine("     <Project></Project>");
            //    sb.AppendLine("     <Name>" + projectName + ".Models</Name>");
            //    sb.AppendLine("     </ProjectReference>");
            //    sb.AppendLine(" </ItemGroup>");
            //}
            //else if (pageType == 3)
            //{
            //    sb.AppendLine(" <ItemGroup>");
            //    sb.AppendLine("     <ProjectReference Include=\"..\\" + projectName + ".Interfaces\\" + projectName + ".Interfaces.csproj\">");
            //    sb.AppendLine("     <Project>{B353B858-7123-4C98-" + GetPartString(projectId, 2, 4) + "-" + GetPartString(projectId, 2, 12) + "}</Project>");
            //    //sb.AppendLine("     <Project></Project>");
            //    sb.AppendLine("     <Name>" + projectName + ".Interfaces</Name>");
            //    sb.AppendLine("     </ProjectReference>");
            //    sb.AppendLine("     <ProjectReference Include=\"..\\" + projectName + ".Models\\" + projectName + ".Models.csproj\">");
            //    sb.AppendLine("     <Project>{B353B858-7123-4C98-" + GetPartString(projectId, 1, 4) + "-" + GetPartString(projectId, 1, 12) + "}</Project>");
            //    //sb.AppendLine("     <Project></Project>");
            //    sb.AppendLine("     <Name>" + projectName + ".Models</Name>");
            //    sb.AppendLine("     </ProjectReference>");

            //    sb.AppendLine(" </ItemGroup>");
            //}
            sb.AppendLine(" <Import Project=\"$(MSBuildToolsPath)\\Microsoft.CSharp.targets\" />");
            sb.AppendLine("</Project>");
            return sb.ToString();
        }

        public static string GetPartString(int projectId, int pageType, int length)
        {
            string part = "";
            if (pageType == 1)
            {
                part = projectId.ToString();
            }
            else if (pageType == 2)
            {
                part = "I" + projectId.ToString();
            }
            else
            {
                part = "M" + projectId.ToString();
            }
            while (part.Length < length)
            {
                part += "0";
            }
            part = part.Substring(0, length);
            return part;
        }
    }
}